﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;

namespace mamnontuoitho7.backend
{
    public partial class KidWall : System.Web.UI.Page
    {
        protected mamnontuoitho7.AppCode.KidWall _kidWall = new mamnontuoitho7.AppCode.KidWall();
        protected DataTable _dttKidWall;
        private DataRow _dtrKidWall;
        protected General _general = new General();

        protected void Page_Load(object sender, EventArgs e)
        {
            //Check Role Admin
            if (Session["memberID"].ToString() != "" && (ConstantClass.permissionInfo).IndexOf(Session["role"].ToString() + ",") != -1)
            {
                try
                {
                        if (Request.QueryString["KidID"] != null && Request.QueryString["KidFullName"] != null && Request.QueryString["hidNamePage"] != null &&
                            Request.QueryString["hidClassID"] != null && Request.QueryString["hidClassName"] != null && Request.QueryString["hidParentID"] != null)
                        {
                            hidKidID.Value = Request.QueryString["KidID"].ToString();
                            hidKidFullName.Value = Request.QueryString["KidFullName"].ToString();
                            hidNamePage.Value = Request.QueryString["hidNamePage"].ToString();
                            hidClassID.Value = Request.QueryString["hidClassID"].ToString();
                            hidClassName.Value = Request.QueryString["hidClassName"].ToString();
                            hidParentID.Value = Request.QueryString["hidParentID"].ToString();

                            //Update Kid Wall
                            if (Request.Form["hidEditKidWallID"] != null)
                            {
                                hidKidWallID.Value = Request.Form["hidEditKidWallID"].ToString();
                               _dtrKidWall = _kidWall.GetOneRecordOfKidWall(Request.Form["hidEditKidWallID"].ToString());
                               txtWall.Text = _dtrKidWall["Content"].ToString();
                            }

                            //Delete Kid Wall
                            if (Request.Form["hidKidWallID"] != null)
                            {
                                _kidWall.DeleteKidWall(Request.Form["hidKidWallID"].ToString());
                            }

                            //Get Heal Information of Kid
                            _dttKidWall = _kidWall.GetKidWall(hidKidID.Value);
                        }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorAdminPage);
                }
            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }

        protected void btnWallCreate_Click(object sender, EventArgs e)
        {
            hidKidWallID.Value = "";
            txtWall.Text = "";
        }

        protected void btnWallSave_Click(object sender, EventArgs e)
        {

            try
            {
                //Insert Kid Wall into database
                if (hidKidWallID.Value == "")
                {
                    bool check = _kidWall.KidWallCreate(Session["memberID"].ToString(), hidKidID.Value, txtWall.Text.TrimEnd());

                    if (!check)
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Có lỗi hệ thống khi lưu thông tin.\n Xin vui lòng làm lại.')", true);
                    }
                    else
                    {
                        txtWall.Text = "";
                        //Get Heal Information of Kid
                        _dttKidWall = _kidWall.GetKidWall(hidKidID.Value);
                    }
                }
                //Update Kid Wall
                else
                {
                    bool check = _kidWall.KidWallUpdate(hidKidWallID.Value, Session["memberID"].ToString(), txtWall.Text.TrimEnd());

                    if (!check)
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Có lỗi hệ thống khi lưu thông tin.\n Xin vui lòng làm lại.')", true);
                    }
                    else
                    {
                        //Get Heal Information of Kid
                        _dttKidWall = _kidWall.GetKidWall(hidKidID.Value);
                    }

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }

        }
    }
}